package com.issac.vhr.service;

import com.issac.vhr.entity.Employee;
import com.issac.vhr.entity.MailConstants;
import com.issac.vhr.entity.Mail_send_log;
import com.issac.vhr.entity.RespPageBean;
import com.issac.vhr.mapper.EmployeeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.UpdateConversionTimeUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class EmployeeService {

    public static final Logger logger = LoggerFactory.getLogger(EmployeeService.class);

    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    MailSendLogService mailSendLogService;

    public RespPageBean getEmployeeByPage(Integer page, Integer size, Employee employee, Date[] beginDate) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<Employee> data = employeeMapper.getEmployeeByPage(page, size, employee, beginDate);
        Long total = employeeMapper.getTotal(employee, beginDate);
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setData(data);
        respPageBean.setTotal(total);
        return respPageBean;
    }

    /*
     * 处理合同年限
     *
     * */
    public Integer addEmp(Employee employee) {
        employee.setContractTerm(UpdateConversionTimeUtils.conversionTime(employee.getEndContract(), employee.getBeginContract()));
        int result = employeeMapper.insertSelective(employee);
        //发送入职邮件
        if (result == 1) {
            Employee emp = employeeMapper.getEmployeeById(employee.getId());
            //生成消息ID
            String msgId = UUID.randomUUID().toString();
            Mail_send_log mailSendLog = new Mail_send_log();
            mailSendLog.setMsgId(msgId);
            mailSendLog.setCreateTime(new Date());
            mailSendLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            mailSendLog.setRouteKey(MailConstants.MAIL_ROUTING_KEY_NAME);
            mailSendLog.setEmpId(emp.getId());
            mailSendLog.setTryTime(new Date(System.currentTimeMillis() + 1000 * 60 * MailConstants.MSG_TIMEOUT));

            mailSendLogService.insert(mailSendLog);
            // CorrelationData(msgId)唯一标记符 rabbitMQconfig的data
            rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME, MailConstants.MAIL_ROUTING_KEY_NAME,emp, new CorrelationData(msgId));
        }
        return result;
    }

    public String getMaxWorkID() {
        return String.format("%08d", employeeMapper.getMaxWorkID() + 1);
    }

    public Integer deleteEmpById(Integer id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    /*
     * 处理合同年限
     *
     * */
    public Integer updateEmp(Employee employee) {
        employee.setContractTerm(UpdateConversionTimeUtils.conversionTime(employee.getEndContract(), employee.getBeginContract()));
        return employeeMapper.updateByPrimaryKeySelective(employee);
    }


    public Integer addEmps(List<Employee> employeesList) {//不插入第一列
        return employeeMapper.addEmps(employeesList);
    }

    public RespPageBean getEmployeeByPageWithSalary(Integer page, Integer size) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<Employee> list = employeeMapper.getEmployeeByPageWithSalary(page, size);
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setData(list);
        respPageBean.setTotal(employeeMapper.getTotal(null, null));
        return respPageBean;
    }

    /*
        replace into empsalary (eid, sid) VALUES (10,10)
    *   eid存在 就更新该eid对应的sid 否则就插入一条新的eid sid
    *
    * */
    public Integer updateEmployeeSalaryById(Integer eid, Integer sid) {
        return employeeMapper.updateEmployeeSalaryById(eid, sid);
    }

    public Employee getEmployeeById(Integer empId) {
        return employeeMapper.getEmployeeById(empId);
    }
}
